
package com.zjff.myshop.front.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.stp.StpUtil;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.zjff.myshop.entity.Address;
import com.zjff.myshop.exception.BusinessException;
import com.zjff.myshop.front.pojo.form.AddressInsertForm;
import com.zjff.myshop.front.pojo.form.AddressUpdateForm;
import com.zjff.myshop.front.pojo.vo.AddressVO;
import com.zjff.myshop.front.service.AddressService;
import com.zjff.myshop.util.BeanUtils;
import com.zjff.myshop.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@CrossOrigin
@RestController
@Api(value = "v1", tags = "06.地址模块")
@RequestMapping("/app/v1/address")
public class AddressController {
    private static final Logger logger = LoggerFactory.getLogger(AddressController.class);
    @Resource
    private AddressService addressService;

    /**
     * 增加地址
     */
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "增加地址", notes = "增加我的新的收货地址")
    @ApiImplicitParam(name = "form", value = "增加地址参数", dataType = "AddressInsertForm", required = true)
    @SaCheckLogin
    @PostMapping("/insert")
    public Result insert(@RequestBody AddressInsertForm form) {
        Integer userId = StpUtil.getLoginIdAsInt();
        //数据准备
        Address address = new Address();
        BeanUtils.copyProperties(form, address);
        address.setUserId(userId);

        //增加
        if (addressService.insert(address)) {
            return Result.ok();
        }
        return Result.error();
    }

    /**
     * 删除收货地址
     */
    @ApiOperationSupport(order = 10)
    @ApiOperation(value = "删除收货地址", notes = "删除我的收货地址，参数为地址id")
    @ApiImplicitParam(name = "id", value = "地址id", dataType = "Integer", required = true)
    @SaCheckLogin
    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable("id") Integer id) {
        Integer userId = StpUtil.getLoginIdAsInt();
        //检查地址
        Address addressDB = addressService.selectByPrimaryKey(id);
        if (!userId.equals(addressDB.getUserId())) {
            throw new BusinessException("业务数据不是你的，禁止该操作");
        }
        //删除
        if (addressService.deleteById(id)) {
            return Result.ok();
        }
        return Result.error();
    }

    /**
     * 更改地址
     */
    @ApiOperationSupport(order = 20)
    @ApiOperation(value = "更改地址", notes = "更改我的收货地址")
    @ApiImplicitParam(name = "form", value = "更改地址时参数", dataType = "AddressUpdateForm", required = true)
    @SaCheckLogin
    @PostMapping("/update")
    public Result updateAddress(@RequestBody AddressUpdateForm form) {
        Integer userId = StpUtil.getLoginIdAsInt();
        //检查地址
        Address address = addressService.selectByPrimaryKey(form.getId());
        if (!userId.equals(address.getUserId())) {
            throw new BusinessException("业务数据不是你的，禁止该操作");
        }
        //数据准备
        BeanUtils.copyProperties(form, address);
        address.setUpdateTime(new Date());

        //更改
        if (addressService.update(address)) {
            return Result.ok();
        }
        return Result.error();
    }

    /**
     * 查询收货地址详情 ById
     */
    @ApiOperationSupport(order = 30)
    @ApiOperation(value = "查询收货地址详情", notes = "查询我的收货地址详情，参数为地址id")
    @ApiImplicitParam(name = "id", value = "地址id", dataType = "Integer", required = true)
    @SaCheckLogin
    @GetMapping("/{id}")
    public Result<AddressVO> getById(@PathVariable("id") Integer id) {
        Integer userId = StpUtil.getLoginIdAsInt();
        //查询数据，并检查
        Address address = addressService.selectByPrimaryKey(id);
        if (!userId.equals(address.getUserId())) {
            throw new BusinessException("业务数据不是你的，禁止该操作");
        }
        //数据格式转换，并返回
        AddressVO addressVO = new AddressVO();
        BeanUtils.copyProperties(address, addressVO);
        return Result.ok(addressVO);
    }

    /**
     * 查询默认收货地址
     */
    @ApiOperationSupport(order = 31)
    @ApiOperation(value = "查询默认收货地址", notes = "查询我的默认收货地址，如果传递了id就查询id对应的收货地址，如果没有传递id就查询默认的收货地址")
    @ApiImplicitParam(name = "id", value = "地址id", dataType = "Integer", required = true)
    @SaCheckLogin
    @GetMapping("/default")
    public Result<AddressVO> getDefaultAddress(Integer id) {
        Integer userId = StpUtil.getLoginIdAsInt();
        Address address;
        if (id == null) {
            //参数为空，则返回默认地址;
            address = addressService.selectDefaultByUserId(userId);
        } else {
            //参数不为空，则返回id对应的地址
            address = addressService.selectByPrimaryKey(id);
            //检查
            if (!userId.equals(address.getUserId())) {
                throw new BusinessException("业务数据不是你的，禁止该操作");
            }
        }
        //数据格式转换，并返回
        AddressVO addressVO = new AddressVO();
        BeanUtils.copyProperties(address, addressVO);
        return Result.ok(addressVO);
    }

    /**
     * 查询我的收货地址列表
     */
    @ApiOperationSupport(order = 32)
    @ApiOperation(value = "我的收货地址列表", notes = "返回我的收货地址列表")
    @SaCheckLogin
    @GetMapping("/list")
    public Result<List<AddressVO>> addressList() {
        Integer userId = StpUtil.getLoginIdAsInt();
        //查询数据，并返回
        return Result.ok(addressService.selectListByUserId(userId));
    }
}
